﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using GSQ.BusinessEntities;
using GSQ.BusinessLogic;

namespace UIapp.Busquedas
{
    public partial class frmRutas : Plantillas.frmFindPlantilla
    {
        public frmRutas()
        {
            InitializeComponent();
            this.entityCollection = new List<RUTAS>();
        }

        #region Variables
        List<RUTAS> entityCollection;
        #endregion

        #region Tratamiento de eventos
        private void txtBuscar_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                e.Handled = true;
                this.backgroundWorker.RunWorkerAsync();
                Comun.ShowLoadingFRM();
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Listar();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Comun.CloseLoadingFRM();
            if (e.Error != null)
                Comun.ShowMessage(e.Error);
            else
                this.bindingSource.DataSource = this.entityCollection.ToList();
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (this.entityCollection.Count > 0)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }
        #endregion

        #region Metodos auxiliares
        public void Listar()
        {
            this.entityCollection.Clear();
            if (this.txtBuscar.Text.Trim() == string.Empty)
            {
                this.entityCollection = new RutasBL().GetAll();
            }
            else
            {
                this.entityCollection = new RutasBL().GetByDescripcion(this.txtBuscar.Text.Trim());
            }
        }
        #endregion
    }
}
